package util;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateManager {
	private static SessionFactory sessionFactory;

	private static void init() {
		Configuration configuration = new Configuration().configure();
		ServiceRegistry registry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
		sessionFactory = configuration.buildSessionFactory(registry);
	}
	
	public static void destory() {
		if (sessionFactory != null)
			sessionFactory.close();
	}
	
	public static Session getSession() {
		if (sessionFactory == null)
			init();
		return sessionFactory.openSession();
	}
}
